/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package motiveai.tests;

import java.util.Random;
import motiveai.factors.Environment;

/**
 *
 * @author oxplay
 */
public class Test4DamageCalculator {

    Random generator = new Random();
    Random generator2 = new Random();

    public int attack(String attackType, Test4Agent agent, Environment environment) {
        if (environment.getIntegerFactor("closeEnemyType") > 0) {
            environment.setBooleanFactor("isRested", false);
            environment.setBooleanFactor("isBored", false);
            agent.getSpatial().getControl(MoveControl.class).moveTo(agent.getSpatial().getLocalTranslation());
            int damage = makeDamage(attackType, agent, agent.closeEnemy);
            return damage;
        }
        return 0;
    }

    private int makeDamage(String attackType, Test4Agent attacker, Test4Agent defender) {
        int damage = 0;
        if (attackType.equals("normal")) {
            damage = 12;
            if (defender.agentType == 1) {
                damage = 30;
            }
        } else if (attackType.equals("power")) {
            damage = 15;
            if (attacker.agentType == 4) {
                damage = 30;
            }
            if (defender.agentType == 2 || defender.agentType == 3) {
                int random1 = generator2.nextInt(3); // 0-2
                if (random1 >= 1) {
                    damage = 0;
                }
            }
        } else if (attackType.equals("critical")) {
            int random1 = generator2.nextInt(2); // 0-1
            int random2 = generator.nextInt(5); // 0-4
            damage = 40;
            if (random1 > 0) {
                damage = 0;
            }
            if (defender.agentType == 3) {
                if (random2 >= 1) {
                    damage = 0;
                }
            }
        }
        defender.health -= damage;
        if (defender.health <= 0) {
            defender.remove();
        }
        return damage;
    }
}
